<template>
	<view class="nav-bar">
		<u-navbar
			:title="titleText"
			:autoBack="false"
			@leftClick="leftClick"
			@rightClick="showPop = !showPop"
		>
			<view class="nav-left-slot" slot="left">{{leftText}}</view>
			<view :class="{'nav-right-slot': true, 'rotateSetting': showPop}" slot="right">
				<image src="@/static/imgs/setting.png" alt="">
			</view>
		</u-navbar>
		 <!-- 自动计算app头部高度 -->
		<view class="status_bar" style="height: var(--status-bar-height); width: 100%;"></view>
		<u-transition :show="showPop" mode="fade-down" :class="{'msg-popup': true}">
			<view class="pon-user">
				<text>作业员:</text>
				<text>{{person}}</text>
			</view>
			<view class="logOut">
				<text @click="logOut">注销</text>
			</view>
		</u-transition>
	</view>
</template>

<script>
	let _this = this
	export default {
		name:"navbar",
		props: {
			leftText: {
				type: String,
				default: '返回'
			},
			titleText: {
				type: String,
				default: '主页'
			}
		},
		data() {
			return {
				showPop: false,
				person: ''
			};
		},
		created() {
			this.person = uni.getStorageSync('user')
		},
		methods: {
			// 导航栏左边按钮点击
			leftClick() {
				if (this.leftText === '注销') {
					this.logOut()
				} else if (this.leftText === '主页') {
					this.goIndex()
				} else if (this.leftText === '返回') {
					this.goBack()
				} else {
					let canNavBack = getCurrentPages()
					if( canNavBack && canNavBack.length === 1) {  
							history.back();
					} else {  
						uni.navigateBack() 
					}
				}
			},
			// 注销
			logOut() {
				uni.showModal({
					title: '提示',
					content: '确认注销登录信息？',
					success:res => {
						if (res.confirm) {
							uni.reLaunch({
								url:'/pages/login/index'
							})
						}
					}
				});
			},
			// 返回主页
			goIndex() {
				uni.redirectTo({
					url:'/pages/home/index'
				})
			},
			// 返回上一页
			goBack() {
				const params = uni.getStorageSync('backParams')
				uni.showModal({
					title: '提示',
					content: '确定放弃当前操作并返回？',
					success:res => {
						if (res.confirm) {
							if (params) {
								uni.removeStorageSync('backParams');
								uni.redirectTo({
									url:`/pages/wms/detail?tabId=${params.tabId}&type=${params.titleText}`
								})
							} else {
								let canNavBack = getCurrentPages()
								if( canNavBack && canNavBack.length === 1) {
										history.back();
								} else {  
									uni.navigateBack() 
								}
							}
						}
					}
				});
			}
		}
	}
</script>

<style lang="scss">
.nav-left-slot {
	color: #337ab7;
}
.rotateSetting {
	transform: rotate(180deg)
}
.nav-right-slot {
	transition: 0.5s;
	image{
		width: 30px;
		height: 30px;
		vertical-align: middle;
	}
}
.msg-popup {
	width: 100%;
	position: absolute;
	top: calc(50px + var(--status-bar-height));
	left: 0;
	padding: 30rpx 20rpx 30rpx 80rpx;
	background-color: #fff;
	box-sizing: border-box;
	border-bottom: 1px solid #dadadc;
	z-index: 1;
	.logOut {
		text-align: right;
		color: #337ab7;
	}
	.pon-user {
		margin-bottom: 10px;
		margin-top: 5px;
	}
}
</style>